<?php

/**
 * Description of DealFlight
 *
 * @author tuancasi
 */
class DealFlight {

    const DEAL = 0;
    const FLIGHT = 1;
    const FEATURED = 1;
    const FEATURED_NO = 0;
    /*
     * Define var for type flights
     */
    const ONEWAY = 1;
    const ROUNDTRIP = 2;
    const MULTICITY = 3;

    /*
     * Define var  for class
     */
    const ECONOMY = 1;
    const BUSINESS = 2;
    const FIRST_CLASS = 3;
    const PREMIUM_ECONOMY = 4;

    /**
     *      
     * @param int $typeTravel
     * @param int $userCreate
     * @param string $name
     * @param string $description
     * @param array $image
     * @param float $price
     * @param float $discount
     * @param int $startTime
     * @param int $endTime
     * @param int $isFeature
     * @param int $order
     */
    public static function add($typeTravel, $userCreate, $name, $description, $image, $price, $discount, $startTime,
	    $endTime, $isFeature, $order, $category, $type_flights, $startLocation,$endLocation,$Adult,$children,
	    $infant,$class,$city) {
        $sImages = serialize($image);
       
        global $wpdb;
        $dsstartTime = explode(' ', $startTime);
        $house = $dsstartTime[1];
        $day = date('Y-m-d', strtotime($dsstartTime[0]));
        
        $startTime = $day.' '.$house;
        
        $dsendTime= explode(' ', $endTime);
        $house = $dsendTime[1];
        $day = date('Y-m-d', strtotime($dsendTime[0]));
        if($isFeature == 1)
        {
            self::updateAllisFeature(self::FEATURED_NO);
            
        }
        $endTime = $day.' '.$house;
        $sql = "INSERT INTO `travel_deal_flight`(`type_travel`, `user_create`, `name`, `description`, `image`,
			`price`, `discount`, `start_time`, `end_time`, `create_time`, `is_feature`, `order`,
			category_id,type_flights,start_location,end_location,children,class,city,adult) 
		VALUES(" . $typeTravel . ", " . $userCreate . ", '" . $name . "', '" . $description . "', '" . $sImages . "', " . $price . ",
		    " . $discount . ", '" . $startTime . "', '" . $endTime . "', " . time() . ", " . $isFeature . ", "
                . $order . "," . $category . ",".$type_flights.",'".$startLocation."','".$endLocation."',
		    ".$children.",".$class.",'".$city."',".$Adult.")";            
	
        $wpdb->query($sql);
     
    }

    /**
     * get all deals/flights
     * 
     * @param int $typeTravel
     * @param int $start
     * @param int $limit
     * @return array
     */
    public static function getAll($typeTravel, $start, $limit) {
        global $wpdb;
        $aDesign = $wpdb->get_results('SELECT * FROM `travel_deal_flight` WHERE type_travel = ' . $typeTravel . ' ORDER BY `deal_flight_id` DESC limit ' . $start . ', ' . $limit);
	
        return $aDesign;
    }

    /**
     * get all deals/flights
     * 
     * @param int $typeTravel
     * @param int $start
     * @param int $limit
     * @return array
     */
    public static function countAllbyIdCat($typeTravel, $id_cat) {
        global $wpdb;
        $aDesign = $wpdb->get_results('SELECT  COUNT(A.deal_flight_id) FROM `travel_deal_flight` as A LEFT JOIN travel_category as B  ON  A.`category_id` = B.`category_id` '
                . ' WHERE type_travel = ' . $typeTravel . ' And  A.category_id =' . $id_cat);

        return $aDesign;
    }

    public static function getAllbyIdCat($typeTravel, $start, $limit, $id_cat) {
        global $wpdb;
        $sql = 'SELECT * FROM `travel_deal_flight` as A LEFT JOIN travel_category as B  ON  A.`category_id` = B.`category_id` '
                . ' WHERE type_travel = ' . $typeTravel
                . ' And  A.category_id =' . $id_cat . '
                   ORDER BY `deal_flight_id` DESC limit ' . $start . ', ' . $limit;
        $aDesign = $wpdb->get_results($sql);
        return $aDesign;
    }

    /**
     * count total deals/flights
     * 
     * @param int $typeTravel
     * @return array
     */
    public static function countRecords($typeTravel) {
        global $wpdb;
        $aDesign = $wpdb->get_results('SELECT COUNT(deal_flight_id) AS total_design FROM `travel_deal_flight` WHERE type_travel = ' . $typeTravel);
        return $aDesign;
    }

    /**
     * @param int $id
     * @return array
     */
    public static function get($id) {
        global $wpdb;
        $aDeal = $wpdb->get_results('SELECT * FROM `travel_deal_flight` WHERE deal_flight_id = ' . $id);
        return $aDeal;
    }

    /**
     *      
     * @param int $id
     * @param string $name
     * @param string $description
     * @param array $image
     * @param float $price
     * @param float $discount
     * @param int $startTime
     * @param int $endTime
     * @param int $isFeature
     * @return array
     */
    public static function update($id, $name, $description, $image, $price, $discount, 
	    $startTime, $endTime,$isFeature, $order, $category, $type_flights, $startLocation,$endLocation,
	    $Adult,$children,$infant,$class,$city) {
        global $wpdb;
        $dsstartTime = explode(' ', $startTime);
        $house = $dsstartTime[1];
        $day = date('Y-m-d', strtotime($dsstartTime[0]));
        
        $startTime = $day.' '.$house;
        
        $dsendTime= explode(' ', $endTime);
        $house = $dsendTime[1];
        $day = date('Y-m-d', strtotime($dsendTime[0]));
        
        $endTime = $day.' '.$house;
        $sImage = serialize($image);
        if($isFeature == 1)
        {
            self::updateAllisFeature(self::FEATURED_NO);
            
        }
       
	$sql = "UPDATE `travel_deal_flight` SET name='" . $name . "', description='" . $description . "', 
		image='" . $sImage . "', price=" . $price . ', discount=' . $discount . ', start_time="' . $startTime . '",
		end_time="'. $endTime .'" , create_time=' . time() . ' , is_feature = ' . $isFeature . ' ,  `order` =' . $order . ' , '
		. ' category_id =' . $category . ',
		   type_flights = ' . $type_flights . ',
		    start_location =  "' . $startLocation . '" ,
		    end_location ="' . $endLocation . '",
		    children = '.$children.',infant = '.$infant.',
		    class ='.$class.',
		    city = "'.$city.'",  
		    adult ='.$Adult.'

		WHERE deal_flight_id = ' . $id;
    
         $wpdb->query($sql);
    }
   
    /**
     * @param int $id
     */
    public static function delete($id) {
        global $wpdb;
        $sql = 'DELETE FROM `travel_deal_flight` WHERE `deal_flight_id` = ' . $id;
        $wpdb->query($sql);
    }

    /**
     * @param int $type
     */
    public static function countFeatured($type, $deal_flight_id = -1) {
        global $wpdb;
        $sql = 'SELECT COUNT(`deal_flight_id`) as total_featured FROM `travel_deal_flight` WHERE deal_flight_id<>"' . $deal_flight_id . '" and    is_feature = 1 and `type_travel` = ' . $type;
        //echo $sql;
        $data = $wpdb->get_results($sql);
        return $data;
    }

    /*
     * Get Fiight All Fight
     * 
     */

    public static function getAllFight($type, $start, $limit) {
        global $wpdb;
        $sql = 'SELECT * ,A.name name_flight  FROM `travel_deal_flight` as A LEFT JOIN travel_category as B  ON  A.`category_id` = B.`category_id`'
                . '  WHERE type_travel = ' . $type . ' LIMIT ' . $start . ' , ' . $limit;

        $data = $wpdb->get_results($sql);
        return $data;
    }
    
    public static function getAllFightLimit($type, $start, $limit) {
        global $wpdb;
        $sql = 'SELECT * FROM `travel_deal_flight`  WHERE type_travel = ' . $type . ' LIMIT ' . $start . ' , ' . $limit;

        $data = $wpdb->get_results($sql);
        return $data;
    }

    public static function getSomeFlight($type) {
        global $wpdb;
        $sql = 'SELECT * FROM `travel_deal_flight`'
                . 'WHERE type_travel = ' . $type
                . '  ORDER BY rand() limit 6';
        //echo $sql;
        $data = $wpdb->get_results($sql);
        return $data;
    }

    public static function getFeaturedDeal() {
        global $wpdb;
        $sql = 'SELECT *,A.name name_deal  FROM `travel_deal_flight` as A LEFT JOIN travel_category as B  ON  A.`category_id` = B.`category_id` '
                . ' WHERE type_travel =' . DealFlight::DEAL . ' ORDER BY `order` limit 5';
        $data = $wpdb->get_results($sql);
        return $data;
    }

    /*
     * Get Flight has set image on fight page
     * @author: Tham
     * 
     */

    public static function getFeatureFight() {
        global $wpdb;
        $sql = 'SELECT *,A.name name_deal  FROM `travel_deal_flight` as A LEFT JOIN travel_category as B  ON  A.`category_id` = B.`category_id` '
                . ' WHERE type_travel =' . DealFlight::FLIGHT . ' and is_feature = ' . self::FEATURED . ' ORDER BY `order` limit 4';
        $data = $wpdb->get_results($sql);
        return $data;
    }

    /*
     * Get the last flight of Deal
     */

    public static function getFirstDEAL() {

        global $wpdb;
        $sql = 'SELECT *,A.name name_deal  FROM `travel_deal_flight` as A LEFT JOIN travel_category as B  ON  A.`category_id` = B.`category_id` '
                . ' WHERE type_travel =' . DealFlight::DEAL . ' ORDER BY deal_flight_id DESC limit 1';
        $data = $wpdb->get_results($sql);
        return $data;
    }

    /*
     * Get info TravelDealFlight by ID
     * @param : $deal_flight_id
     * 
     */

    public static function getInfoDealFlightByDealFlightID($deal_flight_id, $type) {
        global $wpdb;
        $sql = 'SELECT *,A.name name_deal  FROM `travel_deal_flight` as A LEFT JOIN travel_category as B  ON  A.`category_id` = B.`category_id` '
                . ' WHERE type_travel =' . $type . ' and  deal_flight_id=' . $deal_flight_id;
        $data = $wpdb->get_results($sql);
        //echo $sql;
        return $data;
    }

    /*
     * Get Deals on right light
     * 
     */

    public static function getDealRight() {

        global $wpdb;
        $aDesign = $wpdb->get_results('SELECT * FROM `travel_deal_flight` WHERE type_travel = ' . self::DEAL . ' '
                . 'ORDER BY `deal_flight_id` DESC limit 5');
        return $aDesign;
    }

    public static function countSearchFlightTravel($text_search) {
        global $wpdb;
        $sql = "SELECT COUNT(deal_flight_id) AS total_design  FROM `travel_deal_flight`  WHERE name like '%" . $text_search . "%' ";
        $aDesign = $wpdb->get_results($sql);

        return $aDesign;
    }

    public static function SearchFlightTravel($text_search, $start, $limit) {
        global $wpdb;
        $sql = "SELECT  *  FROM `travel_deal_flight`  WHERE name like '%" . $text_search . "%' "
                . " LIMIT " . $start . ',' . $limit;
        $aDesign = $wpdb->get_results($sql);

        return $aDesign;
    }
    public static function updateAllisFeature($isFeature){
         
        global $wpdb;
        $sql = 'Update travel_deal_flight SET  is_feature = '.$isFeature.' WHERE is_feature='.self::FEATURED;
        $wpdb->query($sql);
    }

}
